﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Windows.Forms;
using Telerik.WinControls;
using Telerik.WinControls.UI;
using FukiERP.Common.Constants;
using FukiERP.Helper;
using FukiERP.Services.DBContext;
using FukiERP.Services.Helper;
using FukiERP.Common.Helper;

namespace FukiERP.Report
{
    public partial class InventoryReportForm : Telerik.WinControls.UI.RadForm
    {
        public InventoryReportForm()
        {
            InitializeComponent();
        }

        private void InventoryReportForm_Load(object sender, EventArgs e) {
            FukiERPDataContext context = DataAccess.GetDBContext();
            PresentHelper.LoadProductCate(context, _drpProductCate);
            LoadBrand(context);
            _drpInventoryStatus.SelectedIndex = 0;
            
        }

        private void LoadBrand(FukiERPDataContext context) {
            List<ProductBrand> brands = context.ProductBrands.OrderBy(c => c.Name).ToList();
            _drpProductBrand.DataSource = brands;
            _drpProductBrand.DisplayMember = ColumName.Col_Name;
            _drpProductBrand.Text = CommonConstants.DefaultDropDownValue;
        }

        private void _btnViewReport_Click(object sender, EventArgs e) {
            FukiERPDataContext context = DataAccess.GetDBContext();
            var predicate = PredicateBuilder.True<View_Inventory>();
            if (_drpProductCate.SelectedIndex > 0) {
                int cateID = (int)((RadListDataItem)_drpProductCate.SelectedValue).Value;
                predicate = predicate.And(p => p.CateID == cateID);
            }
            if (_drpProductBrand.SelectedIndex > 0) {
                int brandId = ((ProductBrand)_drpProductBrand.SelectedValue).ID;
                predicate = predicate.And(p => p.ProductBrandID == brandId);
            }
            if (_drpInventoryStatus.SelectedIndex > 0) {
                if (_drpInventoryStatus.SelectedIndex == 1) {
                    predicate = predicate.And(p => p.Quantity > 0);
                }
                if (_drpInventoryStatus.SelectedIndex == 2) {
                    predicate = predicate.And(p => p.Quantity <= 0);
                }
            }
            _grdInventory.DataSource = context.View_Inventories.Where(predicate);
        }
    }
}
